var app = getApp();
var address = require('../../utils/city.js')
var amapFile = require('../../libs/amap-wx.js');
var animation
Page({

  /**
   * 页面的初始数据
   * 当前    provinces:所有省份
   * citys选择省对应的所有市,
   * areas选择市对应的所有区
   * provinces：当前被选中的省
   * city当前被选中的市
   * areas当前被选中的区
   */
  data: {
    menuType: 0,
    a: 0,
    begin: null,
    data_url: "",
    status: 1,
    end: null,
    isDefault:'',
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    addname: "",
    details_text: '',
    addnum: "",
    address_det: "",
    location: "",
    address_list: [],
    showPage: false,
    show_search: false,
    chooseed: false,
    disb:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始化动画变量
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      data_url: app.globalData.data_url
    })
    app.getSessionId().then(function () {
      wx.request({
        url: that.data.data_url + "/GPD/getAddress.do",
        data: {
          session_id: app.globalData.sessionId,
          address_id: options.id
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.code == 0){
            var location = res.data.address.longitude + ',' + res.data.address.latitude
            that.setData({
              addname: res.data.address.name,
              addnum: res.data.address.phone,
              areaInfo: res.data.address.city,
              city: res.data.address.city,
              chooseed:true,
              isDefault: res.data.address.isDefault,
              location: location,
              details_text: res.data.address.address,
              address_det: res.data.address.recommond,
              cid: res.data.address.id
            })
          }
        }
      })
    })
    var animation = wx.createAnimation({
      duration: 200
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  },
  // 显示
  showMenuTap: function (e) {
    //获取点击菜单的类型 1点击状态 2点击时间 
    var menuType = e.currentTarget.dataset.type
    // 如果当前已经显示，再次点击时隐藏
    if (this.data.isVisible == true) {
      this.startAnimation(false, -300)
      return
    }
    this.setData({
      menuType: menuType
    })
    this.startAnimation(true, 0)
  },
  hideMenuTap: function (e) {
    this.startAnimation(false, -200)
  },
  // 执行动画
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
  },
  // 选择状态按钮
  selectState: function (e) {
    this.startAnimation(false, -200)
    var status = e.currentTarget.dataset.status
    this.setData({
      status: status
    })
  },
  // 日志选择
  bindDateChange: function (e) {
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        begin: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        end: e.detail.value
      })
    }
  },
  sureDateTap: function () {
    this.data.pageNo = 1
    this.startAnimation(false, -200)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  addname: function (e) {
    this.setData({
      addname: e.detail.value
    })
  },
  addnum: function (e) {
    this.setData({
      addnum: e.detail.value
    })
  },
  addressDet: function (e) {
    this.setData({
      address_det: e.detail.value
    })
  },
  subAddress: function () {
    var that = this;
    if (that.data.addname == "") {
      wx.showToast({
        title: '姓名不能为空',
        image: '/images/close.png'
      })
      return
    }
    if (that.data.addnum == "") {
      wx.showToast({
        title: '电话不能为空',
        image: '/images/close.png'
      })
      return
    }
    if (that.data.location == "") {
      wx.showToast({
        title: '收货地址不能为空',
        image: '/images/close.png'
      })
      return
    }
    if (that.data.address_det == "") {
      wx.showToast({
        title: '详细地址不能为空',
        image: '/images/close.png'
      })
      return
    }
    this.setData({
      disb:true
    })
    var location = that.data.location.split(',')
    app.getSessionId().then(function () {
      wx.request({
        method: 'post',
        url: that.data.data_url + "/GPD/addReceivedAddress.do?session_id=" + app.globalData.sessionId,
        data: {
          name: that.data.addname,//姓名
          phone: that.data.addnum,//电话
          city: that.data.city,//地址
          address: that.data.details_text,
          isDefault: that.data.isDefault,
          longitude: location[0],
          latitude: location[1],
          recommond: that.data.address_det,//详细地址
          id: that.data.cid,//辨别默认收货地址用的cid
        },
        success: function (res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: '收货地址保存成功',
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 1500)
          } else {
            wx.showToast({
              title: '服务器繁忙，请稍后再试',
              icon: 'none'
            })
            that.setData({
              disb:true
            })
          }
        }
      })
    })
  },
  get_address: function (e) {
    var that = this
    var keywords = e.detail.value;
    this.keywords = e.detail.value
    var myAmapFun = new amapFile.AMapWX({ key: '01a4d722c52ba69ce26a8214feea6d11' });
    myAmapFun.getInputtips({
      keywords: keywords,
      city: that.data.city,
      citylimit: true,
      success: function (data) {
        if (data && data.tips) {
          var tips = data.tips.filter(function (item) {
            if (item.location) {
              return true
            } else {
              return false
            }
          })
          that.setData({
            address_list: tips,
            show_search: Boolean(keywords)
          })
        }
      }
    })
  },
  choose_data: function (e) {
    var details_text = e.currentTarget.dataset.address + e.currentTarget.dataset.name
    this.setData({
      details_text: details_text,
      showPage: false,
      location: e.currentTarget.dataset.location,
      chooseed: true
    })
  },
  open: function () {
    if (this.data.city == "") {
      wx.showToast({
        title: '请先选择城市',
        image: '/images/close.png'
      })
      return
    }
    this.setData({
      showPage: true,
    })
  },
  cancal: function () {
    this.setData({
      showPage: false,
    })
  },
  search: function () {
    var that = this
    var myAmapFun = new amapFile.AMapWX({ key: '01a4d722c52ba69ce26a8214feea6d11' });
    var keywords = this.keywords;
    myAmapFun.getInputtips({
      keywords: keywords,
      city: that.data.city,
      citylimit: true,
      success: function (data) {
        if (data && data.tips) {
          var tips = data.tips.filter(function (item) {
            if (item.location) {
              return true
            } else {
              return false
            }
          })
          that.setData({
            address_list: tips,
          })
        }
      }
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.bottom(0).step()
    } else {
      that.animation.bottom(-300).step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    that.setData({
      city: that.data.citys[value[1]].name
    })
  },
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },

})